<?php
class User_model extends CI_Model
{
	private $TblName = 'tbl_user';//Ten bang khai bao cac thanh vien
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	function register_user($userName,$password)
	{
		$sha1_password=sha1($password);
		$query_str="insert into ".$this->TblName."(user_name,password)values(?,?)";
		$this->db->query($query_str,array($userName,$sha1_password));
	}
	function check_user($userName)
	{
		$query_str="select * from ".$this->TblName ." where user_name=?";
		$result=$this->db->query($query_str,$userName);
		if($result->num_rows()>0)
		{
			return true;
		}else
			return false;
	}
	function check_Email($email)
	{
		$query_str="select * from ".$this->TblName ." where use_email=?";
		//echo $query_str;
		//echo $email;
		$result=$this->db->query($query_str,$email);
		//echo $result->num_rows();
		if($result->num_rows()>0)
		{
			return true;
		}else
			return false;
	}
 
	function get($select = "*", $where = "")
	{
		$this->db->cache_off();
		$this->db->select($select);
		if($where && $where != "")
		{
			$this->db->where($where);
		}
		#Query
		$query = $this->db->get($this->TblName);
		$result = $query->row();
		$query->free_result();
		return $result;
		}
	
	function fetch($select = "*", $where = "", $order = "use_id", $by = "DESC", $start = -1, $limit = 0)
	{
		$this->db->cache_off();
		$this->db->select($select);
		if($where && $where != "")
		{
		$this->db->where($where, NULL, false);
		}
		if($order && $order != "" && $by && ($by == "desc" || $by == "asc"))
		{
			$this->db->order_by($order, $by);
		}
		if((int)$start >= 0 && $limit && (int)$limit > 0)
		{
		$this->db->limit($limit, $start);
		}
		#Query
		$query = $this->db->get($this->TblName);
		$result = $query->result();
		$query->free_result();
		return $result;
		}
	
		function fetch_join($select = "*", $join, $table, $on, $where = "", $order = "use_id", $by = "DESC", $start = -1, $limit = 0, $distinct = false)
		{
		$this->db->cache_off();
		$this->db->select($select);
		if($join && ($join == "INNER" || $join == "LEFT" || $join == "RIGHT") && $table && $table != "" && $on && $on != "")
		{
		$this->db->join($table, $on, $join);
		}
		if($where && $where != "")
		{
		$this->db->where($where, NULL, false);
		}
		if($order && $order != "" && $by && ($by == "DESC" || $by == "ASC"))
		{
		$this->db->order_by($order, $by);
		}
		if((int)$start >= 0 && $limit && (int)$limit > 0)
		{
		$this->db->limit($limit, $start);
		}
		if($distinct && $distinct == true)
		{
		$this->db->distinct();
		}
		#Query
		$query = $this->db->get($this->TblName);
		$result = $query->result();
		$query->free_result();
		return $result;
		}
	
		function add($data)
		{
			return $this->db->insert($this->TblName, $data);
		}

		
		function update($data, $where = "")
		{
		if($where && $where != "")
		{
		$this->db->where($where);
		}
		return $this->db->update($this->TblName, $data);
		}
	
		function delete($value, $field = "use_id", $in = true)
		{
		if($in == true)
		{
		$this->db->where_in($field, $value);
		}
		else
		{
		$this->db->where($field, $value);
		}
		return $this->db->delete($this->TblName);
		}
}

?>